//
//  NSObject+MD5Utils.m
//  runtime
//
//  Created by eport on 2020/12/16.
//

#import "NSObject+MD5Utils.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSObject (MD5Utils)


+(NSString*)MD5:(NSString *)input{
    NSString* s = [self makeMd5:input];
    NSString* s1 = [s substringToIndex:16];
    NSString* s2 =[s substringFromIndex:16];
    s1 = [self makeMd5:s1];
    s2 = [self makeMd5:s2];
    NSMutableString* md5String = [NSMutableString stringWithString:s1];
    [md5String appendString:s2];
    for(int i =0;i<100;i++){
        md5String = [self makeMd5WithMutaleString:md5String];
    }
    return md5String;
}

+ (NSString*)makeMd5:(NSString*) str{
    const char * pointer = [str UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(pointer, (CC_LONG)strlen(pointer), md5Buffer);
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [string appendFormat:@"%02x",md5Buffer[i]];
    return string;
}

+ (NSMutableString*)makeMd5WithMutaleString:(NSMutableString*) str{
    const char * pointer = [str UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(pointer, (CC_LONG)strlen(pointer), md5Buffer);
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [string appendFormat:@"%02x",md5Buffer[i]];
    return string;
}


@end
